package leetcode;

import java.util.HashMap;
import java.util.Objects;

/**
 * 描述:219. 存在重复元素 II
 *
 * @author Madison You
 * @created 23:15
 */
public class Easy_219_ContainsNearbyDuplicate {
    public static void main(String[] args) {
        int[] ints = {1, 0, 1, 1};
        new Easy_219_ContainsNearbyDuplicate().containsNearbyDuplicate(ints, 1);

    }
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            Integer integer = map.get(nums[i]);
            if (integer == null) {
                map.put(nums[i], i);
            } else {
                if (Math.abs(integer - i) <= k) {
                    return true;
                }
                map.put(nums[i], i);
            }
        }
        return false;
    }
}
